package cn.stylefeng.guns.modular.goodsMcJst.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.goodsMcJst.entity.GoodsMcJst;
import cn.stylefeng.guns.modular.goodsMcJst.entity.GoodsMcJstExcelItem;
import cn.stylefeng.guns.modular.goodsMcJst.mapper.GoodsMcJstMapper;
import cn.stylefeng.guns.modular.goodsMcJst.model.params.GoodsMcJstParam;
import cn.stylefeng.guns.modular.goodsMcJst.model.result.GoodsMcJstResult;
import  cn.stylefeng.guns.modular.goodsMcJst.service.GoodsMcJstService;
import cn.stylefeng.guns.modular.packreview.entity.PackReview;
import cn.stylefeng.guns.modular.packreview.entity.PackReviewExcelItem;
import cn.stylefeng.guns.modular.packreview.model.params.PackReviewDetailsParam;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.stylefeng.guns.modular.location.excel.ExcelUtils.getCellValue;
import static cn.stylefeng.guns.sys.core.shiro.ShiroKit.getUser;

/**
 * <p>
 * 猫超聚水潭商品对应关系 服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-07-24
 */
@Service
public class GoodsMcJstServiceImpl extends ServiceImpl<GoodsMcJstMapper, GoodsMcJst> implements GoodsMcJstService {

    @Override
    public void add(GoodsMcJstParam param){
        GoodsMcJst entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GoodsMcJstParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(GoodsMcJstParam param){
        GoodsMcJst oldEntity = getOldEntity(param);
        GoodsMcJst newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GoodsMcJstResult findBySpec(GoodsMcJstParam param){
        return null;
    }

    @Override
    public List<GoodsMcJstResult> findListBySpec(GoodsMcJstParam param){
        return this.baseMapper.customListNew(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(GoodsMcJstParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData getExceldata(MultipartFile file, GoodsMcJstParam goodsMcJstParam, ResponseData data) throws IOException {
        String fileName = file.getOriginalFilename();

        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            data.setMessage("上传文件格式不正确");
            data.setSuccess(false);
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);

        List<GoodsMcJstExcelItem> list = new ArrayList<GoodsMcJstExcelItem>();
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }

            GoodsMcJstExcelItem goodsMcJstExcelItem = new GoodsMcJstExcelItem();
            String  mcGoodsCode = getCellValue(row.getCell(0));
            if (mcGoodsCode == null || mcGoodsCode.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setMcGoodsCode(mcGoodsCode);//猫超商品编码

            String mcGroupCode = getCellValue(row.getCell(1));
            if (mcGroupCode == null || mcGroupCode.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setMcGroupCode(mcGroupCode);//组合款式编码

            String goodsGroupName = getCellValue(row.getCell(2));
            if (goodsGroupName == null || goodsGroupName.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setMcGroupName(goodsGroupName);//组合商品名称

            String goodsCode = getCellValue(row.getCell(3));
            if (goodsCode == null || goodsCode.equals("")){
                continue;
            }

            goodsMcJstExcelItem.setJstGoodsCode(goodsCode);//聚水潭商品编码

            String goodsname = getCellValue(row.getCell(4));
            if (goodsname == null || goodsname.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setJstGoodsName(goodsname);//商品名称(goodsname);//聚水潭商品名称

            String goodsCout = getCellValue(row.getCell(5));
            if (goodsCout == null || goodsCout.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setGoodsCount(goodsCout);//商品数量

            String goodsNsCode = getCellValue(row.getCell(6));
            if (goodsNsCode == null || goodsNsCode.equals("")){
                continue;
            }
            goodsMcJstExcelItem.setNsCode(goodsNsCode.trim());//国标码
            list.add(goodsMcJstExcelItem);
        }

        for (GoodsMcJstExcelItem packReviewItem : list) {
            GoodsMcJst goodsMcJst = new GoodsMcJst();
            BeanUtils.copyProperties(packReviewItem, goodsMcJst);

            //判断是否存在
            GoodsMcJst oldGoodsMcJst = this.baseMapper.selectByMcGoodsCode(goodsMcJst.getMcGoodsCode());
            if (oldGoodsMcJst != null){
                this.baseMapper.deleteByMcGoodsCode(goodsMcJst.getMcGoodsCode());
            }
        }

        for (GoodsMcJstExcelItem packReviewItem : list) {
            GoodsMcJst goodsMcJst = new GoodsMcJst();
            BeanUtils.copyProperties(packReviewItem, goodsMcJst);
            this.saveGoodsMcJst(goodsMcJst);
        }

        data.setSuccess(true);
        data.setMessage("导入成功");
        return data;
    }

    private void saveGoodsMcJst(GoodsMcJst goodsMcJst) {
        this.baseMapper.saveGoodsMcJst(goodsMcJst);
    }

    private Serializable getKey(GoodsMcJstParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GoodsMcJst getOldEntity(GoodsMcJstParam param) {
        return this.getById(getKey(param));
    }

    private GoodsMcJst getEntity(GoodsMcJstParam param) {
        GoodsMcJst entity = new GoodsMcJst();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
